﻿using CCWin.SkinControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win.CSkin.Test
{
    public partial class TextKeybord : Form
    {
        TextBox currentTextBox;
        int currentpos;

        public TextKeybord()
        {
            InitializeComponent();

            foreach (Control ctl in Controls)
            {
                if (ctl.GetType().Equals(typeof(TextBox)))
                    ctl.LostFocus += new EventHandler(ctl_LostFocus);
                else if (ctl.Name == "btnKeyBoardClear")  //退格键
                    ctl.Click += new EventHandler(ctl_ClearClick);
                else if (ctl.GetType().Equals(typeof(SkinButton)) && ctl.Name != "btnSubmit")
                    ctl.Click += new EventHandler(ctl_Click);
                else
                    ctl.Enter += new EventHandler(ctl_Enter);
            }
        }

        void ctl_ClearClick(object sender, EventArgs e)
        {
            if (currentTextBox != null)
            {
                try
                {
                    //删除文本框中前一个字符
                    if (currentpos <= 0)
                    {
                        return;
                    }
                    currentTextBox.Text = currentTextBox.Text.Remove(currentpos - 1, 1);
                    //currentpos -= ((SkinButton)sender).Text.Length;
                    currentpos -= 1;
                    currentTextBox.Focus();
                    currentTextBox.SelectionStart = currentpos + 1;
                    currentTextBox.SelectionLength = 0;
                }
                catch
                {
                    MessageBox.Show("请从正确位置删除！");
                }
            }
            else
                return;
        }

        void ctl_Enter(object sender, EventArgs e)
        {
            currentTextBox = null;
        }

        void ctl_Click(object sender, EventArgs e)
        {
            if (currentTextBox != null)
            {
                currentTextBox.Text = currentTextBox.Text.Insert(currentpos, ((Button)sender).Text);
                currentpos += 1;  // ((SkinButton)sender).Text.Length;
                currentTextBox.Focus();
                currentTextBox.SelectionStart = currentpos;
                currentTextBox.SelectionLength = 0;
            }
            else
                return;
        }

        void ctl_LostFocus(object sender, EventArgs e)
        {
            currentTextBox = (TextBox)sender;
            currentpos = currentTextBox.SelectionStart;
        }

        private void btnBoard_Click(object sender, EventArgs e)
        {
            PopKeyboard f = new PopKeyboard();
            textBox4.Focus();
            f.Show(this);
            this.Activate();
        }

        private void numUD1_Click(object sender, EventArgs e)
        {
            PopKeyboard f = new PopKeyboard();
            f.Show(this);
            this.Activate();
        }

        private void numUD2_Click(object sender, EventArgs e)
        {
            PopKeyboard f = new PopKeyboard();
            f.Show(this);
            this.Activate();
        }

    }
}
